<template>
  <el-dialog
    title="修改收货地址"
    :visible.sync="modifyAddressDialog"
    :before-close="dialogClose"
    @open="resetNewAddress"
  >
    <el-form :model="address" :rules="$rules.address" ref="addressRuleForm">
      <el-form-item label="省市区/县" label-width="100px" prop="city">
        <div class="block">
          <el-cascader
            ref="cityForm"
            style="width: 100%"
            v-model="address.city"
            :options="cityForm"
          ></el-cascader>
        </div>
      </el-form-item>
      <el-form-item label="详细地址" label-width="100px" prop="xiangxiAdd">
        <el-input v-model="address.xiangxiAdd" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogClose">取 消</el-button>
      <el-button type="primary" @click="handleModifyAddress">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import citydata from "@/assets/js/citydata";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    orderId: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      modifyAddressDialog: false,
      form: {},
      cityForm: citydata,
      address: {
        xiangxiAdd: "",
        city: [],
      },
    };
  },
  methods: {
    async handleModifyAddress() {
      try {
        await this.$refs.addressRuleForm.validate();
        let newAddress = {
          consignee_addr: "",
        };
        for (let item of this.address.city) {
          newAddress.consignee_addr += item;
        }
        newAddress.consignee_addr =
          newAddress.consignee_addr + this.address.xiangxiAdd;
        // console.log(newAddress.consignee_addr, this.orderId);
        let result = await this.$api.modifyAddressById(
          this.orderId,
          newAddress
        );
        if(result=='error') return
        this.$message.success("地址修改成功");
        this.$emit("handleModifyAddress", false);
      } catch (error) {
        console.log("修改地址错误:", error);
      }
    },
    dialogClose() {
      this.$emit("update:visible", false);
    },
    resetNewAddress() {
      this.address.xiangxiAdd = "";
      this.$nextTick(() => {
        this.$refs.cityForm.panel.clearCheckedNodes();
      });
    },
  },
  watch: {
    visible() {
      this.modifyAddressDialog = this.visible;
    },
  },
};
</script>